<?php

if(!isset($_SESSION["id_membre"])) {
session_start();
}

/********************************************************
 * Traitement PHP :                                     *
 * Ensemble des fonctions utilisée dans le traitement   *
 * PHP                                                  *
 * *****************************************************/

if( file_exists("../../lib/mysql_connect.php") )
  require("../../lib/mysql_connect.php");
else
  require("../lib/mysql_connect.php");

function get_detail_seance_bilan($id_seance)
{
  $requete = "SELECT nom, charge, serie, repetition FROM atelier_bilan_seance, atelier, exercice_bilan_seance WHERE
    id_seance = '".$id_seance."'
    AND atelier_bilan_seance.id_atelier_bilan_seance  = exercice_bilan_seance.id_atelier_bilan_seance
    AND atelier.id_atelier = atelier_bilan_seance.id_atelier 
    ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "
          <tr>
          <td>".$row['nom']."</td><td>".$row['charge']."</td><td>".$row['serie']."</td><td>".$row['repetition']."</td>
          </tr>

          ";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
    }
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
  }
}

function get_detail_seance($id_seance)
{
  $requete = "SELECT nom, charge, serie, repetition FROM atelier_projet_seance, atelier, exercice_projet_seance WHERE
    id_seance = '".$id_seance."'
    AND atelier_projet_seance.id_atelier_projet_seance  = exercice_projet_seance.id_atelier_projet_seance
    AND atelier.id_atelier = atelier_projet_seance.id_atelier 
    ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "
          <tr>
          <td>".$row['nom']."</td><td>".$row['charge']."</td><td>".$row['serie']."</td><td>".$row['repetition']."</td>
          </tr>

          ";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
    }
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
  }
}

function bilan_seance($id_seance)
{
  $requete = "SELECT ressenti,commentaire FROM bilan_seance WHERE
    id_seance = '".$id_seance."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      $row = mysql_fetch_array($requete_result);
      $res = array( "ressenti" => $row['ressenti'], "commentaire" => $row['commentaire']);
      return $res;

    }else
      return "Bilan de séance non effectué";
  }
  else
    return "Bilan de séance non effectué";

}

function get_seance()
{
  $requete = "SELECT id_seance,date FROM seance WHERE
    id_eleve = '".$_SESSION['id_membre']."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {
         echo '<OPTION VALUE='.$row['id_seance'].'>'.$row['date'].'</OPTION>';
      }
    }else
      {
        return;
      }
  }
  else
  {
    return;
  }

}

/********************************************************
 * Traitement AJAX :                                    *
 * Affichage de la slite des bilans de séance           *
 * pour l'élève                                         *
 * *****************************************************/
 
if( isset($_POST['type']) && $_POST['type'] == "bilan" )
{
  $type = $_POST['type'];
  $erreur = "";
  $id_seance = $_POST['id_seance'];

  if( !isset($id_seance) || empty($id_seance) )
    $erreur = "<div class='erreur_div'>Erreur : Champs séance vide</div>";

  if( $erreur != "" )
    echo $erreur;
  else{

    echo "<div class='contenu_haut'>.: Détail de la séance :.</div><div class='contenu_fond'>
      <div id='table_bilan'>
      <div id='seance_view'><h3>Séance prévue</h3></div>
      <table>
      <tr>
      <th>Nom de l'atelier</td><th>Charge</td><th>Série</td><th>Répétition</td>
      </tr>
      ";
    get_detail_seance($id_seance);

    echo "    
      </table>";

    $bilan = bilan_seance($id_seance);

    echo " <div id='seance_view'><h3>Bilan de séance</h3></div>
      <table>
      <tr>
      <th>Ressenti</td>
      <th>Commentaire</th>
      </tr>
      <tr>
      ";

    if(count($bilan) <= 1) {
      echo "<td>-</td><td>-</td>";
    }else{
      echo "    <td>".$bilan['ressenti']."</td>
        <td>".$bilan['commentaire']."</td>
        ";
    }
    echo " 
      </tr>
      </table>";

    echo " <table>
      <tr>
      <th>Nom de l'atelier</td><th>Charge</td><th>Série</td><th>Répétition</td>
      </tr>
      ";
    get_detail_seance_bilan($id_seance);
    echo " </table></div></div><div class='contenu_bas'></div>";

  }
}
?>
